<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="客户名称">
              <a-input placeholder="请输入客户名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="跟进状态">
              <j-dict-select-tag v-model="queryParam.followup" title="状态" dictCode="cusFollowup" placeholder="请选择" />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="来源">
                <j-dict-select-tag v-model="queryParam.origin" title="来源" dictCode="cusOrigin" placeholder="请选择" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="客户类型">
                <j-dict-select-tag v-model="queryParam.type" title="类型" dictCode="cusType" placeholder="请选择" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="客户等级">
                <j-dict-select-tag v-model="queryParam.level" title="等级" dictCode="cusLevel" placeholder="请选择" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
     <!-- <a-button type="primary" icon="download" @click="handleExportXls('客户')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
        @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> 
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item>
            <a @click="look(record)">查看</a>
          </a-menu-item>
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
     <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
          style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->
      <a-table ref="table" size="middle" bordered rowKey="id" :columns="columns" 
        :dataSource="dataSource"
        :pagination="ipagination" 
        :loading="loading" class="j-table-force-nowrap"
        :scroll="{ x: 1600 }"
        :expandedRowKeys= "expandedRowKeys"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange"
        @expand="handleExpand"
        :customRow="handleClickRow"
      >

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多
              <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="look(record)">查看</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a @click="addExchange(record)">新增往来</a>
              </a-menu-item>
              <!-- <a-menu-item v-show="record.isTransform==0">
                <a @click="addRela(record)">新增联系人</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="addBus(record)">新增商机</a>
              </a-menu-item>
              <a-menu-item v-show="record.isTransform==0">
                <a @click="addCon(record)">签订合同</a>
              </a-menu-item> -->
              <a-menu-item>
                <a @click="handleAppoint(record)">客户分配</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleAbandon(record)">放弃跟进</a>
              </a-menu-item>
              
              <a-menu-item v-show="record.followup!=2">
                <a @click="addSingelOrder(record)">转直销订单</a>
              </a-menu-item>
              <a-menu-item v-show="record.followup!=2">
                <a @click="addMassOrder(record)">转批发订单</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
         <a-table
          slot="expandedRowRender"
          :columns="innerColumns"
          :dataSource="innerData"
          size="middle"
           :loading="loadingInner" 
           :scroll="{ x: 'auto' }"
          bordered
          rowKey="id"
          :pagination="false"
          >
          <div slot="content"  slot-scope="text" >
            <j-ellipsis :value="text"/>
          </div>
           <div slot="remark"  slot-scope="text" >
            <j-ellipsis :value="text"/>
          </div>
        </a-table>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <cus-look ref="lookForm" @ok="modalFormOk"></cus-look>
    <cus-modal ref="modalForm" @ok="modalFormOk"></cus-modal>
    <cus-exchange ref="exchangeForm" @ok="modalFormOk"></cus-exchange>
    <rela-modal ref="relaForm" @ok="modalFormOk"></rela-modal>
    <bus-modal ref="busForm" @ok="modalFormOk"></bus-modal>
    <cus-appoint ref="appointForm" @ok="modalFormOk"></cus-appoint>
    <cus-abandon ref="abandonForm" @ok="modalFormOk"></cus-abandon>
    <cus-order-single ref="orderSingelForm" @ok="modalFormOk"></cus-order-single>
    <cus-order-mass ref="orderMassForm" @ok="modalFormOk"></cus-order-mass>
  </a-card>
</template>

<script>
  import '@/assets/less/TableExpand.less'
  import { getAction } from '@/api/manage'
  import CusLook from './modules/CusLook'
  import CusModal from './modules/CusModal'
  import CusExchange from './/modules/CusExchange'
  import RelaModal from '../rela/modules/RelaModal'
  import BusModal from '../bus/modules/BusModal'
  import CusAppoint from './modules/CusAppoint'
  import CusAbandon from './modules/CusAbandon'
  import CusOrderSingle from './modules/CusOrderSingle'
  import CusOrderMass from './modules/CusOrderMass'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
  import {getPcaText, getPcaCode} from '@/utils/areaUtil'
  import JEllipsis from '@/components/jeecg/JEllipsis'

  export default {
    name: "CusList",
    mixins: [JeecgListMixin],
    components: {
      CusModal,
      CusLook,
      CusExchange,
      BusModal,
      RelaModal,
      CusAppoint,
      CusAbandon,
      CusOrderMass,
      CusOrderSingle,
      JEllipsis
    },
    data() {
      return {
        description: '客户管理页面',
        // 表头
        columns: [{
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function(t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '客户名称',
            align: "center",
            dataIndex: 'name',
            width: 200,
            ellipsis: true
          },
          {
            title: '手机',
            align: "center",
            dataIndex: 'mobile'
          },
          {
            title: '跟进状态',
            align: "center",
            dataIndex: 'followup',
            customRender: (text) => {
              return filterDictTextByCache('cusFollowup', text)
            },
          },
          {
            title: '来源',
            align: "center",
            dataIndex: 'origin',
            customRender: (text) => {
              return filterDictTextByCache('cusOrigin', text)
            },
          },
          {
            title: '客户类型',
            align: "center",
            dataIndex: 'type',
            customRender: (text) => {
              return filterDictTextByCache('cusType', text)
            },
          },
          {
            title: '客户等级',
            align: "center",
            dataIndex: 'level',
            customRender: (text) => {
              return filterDictTextByCache('cusLevel', text)
            },
          },
          {
            title: '区域',
            align: "center",
            dataIndex: 'province',
            width: 180,
            ellipsis: true,
            customRender: (text, record) => {
              return getPcaText(text);
              // return record.province+" "+record.city+ " "+ record.region;
            },
          },
          {
            title: '备注',
            align: "center",
            dataIndex: 'remark',
            width: 200,
            ellipsis: true
          },
          {
            title: '创建人',
            align: "center",
            dataIndex: 'createIdText'
          },
          {
            title: '跟进人',
            align: "center",
            dataIndex: 'ownerUserIdText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: "center",
            fixed: 'right',
            scopedSlots: {
              customRender: 'action'
            },
          }
        ],
        innerColumns:[
		   /* {
            title: '联系人',
            align:"center",
            dataIndex: 'relaIdText'
           },
		   {
            title: '关联商机',
            align:"center",
            dataIndex: 'busIdText'
           }, */
           {
                title: '跟进人',
                 width: 80,
                align:"center",
                dataIndex: 'ownerUserIdText'
               },
		   {
            title: '往来方式',
            align:"center",
             width: 80,
            dataIndex: 'way',
            customRender: (text) => {
              return filterDictTextByCache('exchangeWay', text)
            },
           },
		   {
            title: '联系方式(地址)',
            align:"center",
             width: 80,
            dataIndex: 'phone'
           },
		   {
            title: '计划时间',
             width: 100,
            align:"center",
            dataIndex: 'planDate'
           },
		   {
            title: '状态',
            align:"center",
             width: 100,
            dataIndex: 'status',
            customRender: (text) => {
              return filterDictTextByCache('exchangeStatus', text)
            },
           },
		  
		   {
            title: '来往内容',
            align:"center",
             width: 100,
            dataIndex: 'content',
            scopedSlots: {
              customRender: 'content'
            },
           },
		   {
            title: '备注',
            width: 100,
            align:"center",
            dataIndex: 'remark',
            ellipsis: true,
            scopedSlots: {
              customRender: 'remark'
            },
           },
		   {
            title: '创建人',
             width: 100,
            align:"center",
            dataIndex: 'createIdText'
           },
        ],
        innerData:[],
        url: {
          list: "/crm/cus/list",
          delete: "/crm/cus/delete",
          deleteBatch: "/crm/cus/deleteBatch",
          exportXlsUrl: "crm/cus/exportXls",
          importExcelUrl: "crm/cus/importExcel",
          listByCusId:"crm/exchange/listByCusId"
        },
         expandedRowKeys: [],
         loadingInner:false,
      }
    },
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
       handleExpand(expanded, record){
        this.expandedRowKeys=[];
        this.innerData=[];
        
        if(expanded===true){
          this.loadingInner = true
          this.expandedRowKeys.push(record.id);
          getAction(this.url.listByCusId, {cusId: record.id}).then((res) => {
            if (res.success) {
              this.loadingInner = false
              this.innerData = res.result.records;
            }
          });
        }
      },
      look: function (record) {
        this.$refs.lookForm.look(record);
        this.$refs.lookForm.title = "查看";
        this.$refs.lookForm.disableSubmit = true;
      }, 
      addExchange: function (record) {
        this.$refs.exchangeForm.edit(record);
        this.$refs.exchangeForm.title = "新增往来";
        this.$refs.exchangeForm.disableSubmit = false;
      },
      addRela: function (record) {
        this.$refs.relaForm.edit(record);
        this.$refs.relaForm.title = "新增联系人";
        this.$refs.relaForm.disableSubmit = false;
      },
      addBus: function (record) {
        this.$refs.busForm.edit(record);
        this.$refs.busForm.title = "新增商机";
        this.$refs.busForm.disableSubmit = false;
      },
      handleAppoint: function (record) {
        this.$refs.appointForm.edit(record);
        this.$refs.appointForm.title = "客户分配";
        this.$refs.appointForm.disableSubmit = false;
      },
      handleAbandon: function (record) {
        this.$refs.abandonForm.edit(record);
        this.$refs.abandonForm.title = "放弃跟进";
        this.$refs.abandonForm.disableSubmit = false;
      },
      addSingelOrder: function (record) {
        this.$refs.orderSingelForm.edit(record);
        this.$refs.orderSingelForm.title = "直销订单";
        this.$refs.orderSingelForm.disableSubmit = false;
      },
      addMassOrder: function (record) {
        this.$refs.orderMassForm.edit(record);
        this.$refs.orderMassForm.title = "批量订单";
        this.$refs.orderMassForm.disableSubmit = false;
      },
      handleClickRow(record, index){
        return {
         on: {
          dblclick: () => {
            this.look(record);
          }
         }
        }
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
